package com.software.seakdream.service;

import com.software.seakdream.entity.AudioInfo;
import com.software.seakdream.entity.History;
import com.software.seakdream.entity.HistoryRecord;
import com.software.seakdream.entity.UserInfo;
import com.software.seakdream.repository.AudioInfoRepository;
import com.software.seakdream.repository.FavoriteRepository;
import com.software.seakdream.repository.HistoryRepository;
import com.software.seakdream.repository.UserInfoRepository;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

@Service
public class HistoryServiceImpl implements HistoryService{
    @Autowired
    private HistoryRepository historyRepository;
    @Autowired
    private FavoriteRepository favoriteRepository;
    @Autowired
    private AudioInfoRepository audioInfoRepository;

    @Override
    public History createHistory(History history) {
        historyRepository.createHistory(history);
        return history;
    }

    @Override
    public Boolean deleteHistory(String userId, String audioId) {
        int result = historyRepository.deleteHistory(userId, audioId);
        if (result == 0){
            return false;
        }else {
            return true;
        }
    }


    @Override
    public List<HistoryRecord> getHistoryRecordUsePage(String userId, int page, int pageSize) {
//        List<History> histories = historyRepository.findHistoryInRange(userId,(page-1)*pageSize,pageSize);
////        System.out.println(histories.size());
//        List<HistoryRecord> records = new ArrayList<HistoryRecord>();
//        AudioInfo audioInfo;
//        for(History history:histories){
//            HistoryRecord record = new HistoryRecord();
//            record.setHistoryId(history.getHistoryId());;
//            record.setWhiteNoise(history.getIsWhite() != null);
//            if(record.isWhiteNoise()){
//                int count = 0;
//                if(history.getWhiteNoise1()!=null && !history.getWhiteNoise1().equals("")){
////                    System.out.println("whiteNoise1=" + history.getWhiteNoise1());
//                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise1());
//                    record.setWhiteId1(audioInfo.getAudioId());
//                    record.setWhiteNoiseImg1(audioInfo.getAudioPic());
//                    record.setWhiteNoiseName1(audioInfo.getAudioName());
//                    record.setWhiteNoiseUrl1(audioInfo.getAudioUrl());
//                    count++;
//                }
//                if(history.getWhiteNoise2()!=null && !history.getWhiteNoise2().equals("")){
////                    System.out.println("whiteNoise2=" + history.getWhiteNoise2());
//                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise2());
//                    record.setWhiteId2(audioInfo.getAudioId());
//                    record.setWhiteNoiseImg2(audioInfo.getAudioPic());
//                    record.setWhiteNoiseName2(audioInfo.getAudioName());
//                    record.setWhiteNoiseUrl2(audioInfo.getAudioUrl());
//                    count++;
//                }
//                if(history.getWhiteNoise3()!=null && !history.getWhiteNoise3().equals("")){
////                    System.out.println("whiteNoise2=" + history.getWhiteNoise2());
//                    audioInfo = audioInfoRepository.findAudioInfoById(history.getWhiteNoise3());
//                    record.setWhiteId3(audioInfo.getAudioId());
//                    record.setWhiteNoiseImg3(audioInfo.getAudioPic());
//                    record.setWhiteNoiseName3(audioInfo.getAudioName());
//                    record.setWhiteNoiseUrl3(audioInfo.getAudioUrl());
//                    count++;
//                }
//                record.setWhiteNoiseCount(count);
//            }else{
//                record.setAudioId(history.getAudioId());
//                audioInfo = audioInfoRepository.findAudioInfoById(history.getAudioId());
//                record.setAudioImg(audioInfo.getAudioPic());
//                record.setAudioName(audioInfo.getAudioName());
//                record.setAudioAuthor(audioInfo.getAudioAuthor());
//                record.setAudioUrl(audioInfo.getAudioUrl());
//                record.setAudioType(audioInfo.getType());
//            }
//            records.add(record);
//        }
//        return records;
        List<HistoryRecord> historyRecords = historyRepository.findHistoryInRange(userId,(page-1)*pageSize,pageSize);
        return historyRecords;
    }

    @Override
    public List<History> findHistory(String userId) {
        return historyRepository.findHistory(userId);
    }

    @Override
    public void deleteAllHistory(String userId) {
//        System.out.println(userId);
        historyRepository.deleteAllHistory(userId);
    }

}
